/**
 * 
 */
package com.google.code.cisx;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.code.cisx.model.CommonUpdatableModelEntity;
import com.google.code.cisx.model.project.BuildDefinition;
import com.google.code.cisx.store.StoreService;
import com.google.code.cisx.store.api.StoreException;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @version $Id: DefaultCIService.java 258 2008-11-29 06:23:51Z rahul.thakur.xdev $
 * @since 1.0
 */
public class DefaultCIService implements CIService {

  private static final Log log = LogFactory.getLog(DefaultCIService.class);

  /**
   * {@inheritDoc}
   * 
   * @see com.google.code.cisx.CIService#lookup(com.google.code.cisx.model.CommonCreatedEntity)
   */
  public <T extends CommonUpdatableModelEntity> T lookup(Class<T> klass, Long id) throws CIServiceException {
    try {
      T saved = StoreService.lookup(klass, id);
      return saved;
    } catch (StoreException e) {
      throw new CIServiceException(String.format("Unable to persist %s instance.", klass.getName()), e);
    }
  }

  /**
   * {@inheritDoc}
   * 
   * @see com.google.code.cisx.CIService#add(com.google.code.cisx.model.CommonCreatedEntity)
   */
  @Override
  public <T extends CommonUpdatableModelEntity> T save(Class<T> klass, T entity) throws CIServiceException {
    try {
      T saved = StoreService.save(klass, entity);
      return saved;
    } catch (StoreException e) {
      throw new CIServiceException(String.format("Unable to persist %s instance.", klass.getName()), e);
    }
  }

  /**
   * {@inheritDoc}
   * 
   * @see com.google.code.cisx.CIService#executeBuild(BuildDefinition)
   */
  @Override
  public Long executeBuild(BuildDefinition build) throws CIServiceException {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * {@inheritDoc}
   * 
   * @see com.google.code.cisx.CIService#remove(Classs<T>,
   *      com.google.code.cisx.model.CommonCreatedEntity)
   */
  @Override
  public <T extends CommonUpdatableModelEntity> void delete(Class<T> klass, T entity) throws CIServiceException {
    try {
      StoreService.delete(klass, entity);
    } catch (StoreException e) {
      throw new CIServiceException(String.format("Unable remove %s instance.", entity.getClass().getName()), e);
    }
  }

}
